package mt.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import mt.comm.ClientComm;
/**
 * Creates a GUI which displays both the orders available to buy as well as an history list of all transactions.
 * 
 * @author Grupo 2
 *
 */

public class MicroTraderOnlineGui {

	private JFrame frame;	
	private DefaultListModel<String> historyModel = new DefaultListModel<String>();
	private DefaultListModel<String> marketModel = new DefaultListModel<String>();
	private JScrollPane historyscrollPane = new JScrollPane();
	private JScrollPane marketscrollPane = new JScrollPane();
	private JList<String> historyList = new JList(historyModel);
	private JButton buyButton, sellButton, logoutButton;
	private JList<String> marketList = new JList(marketModel);
	private JPanel historyPanel = new JPanel();
	private JPanel marketPanel = new JPanel();
	private JPanel buttonPanel = new JPanel();
	private String user;
	private ClientComm clientcomm;
	
	/**
	 * Add components to the frame which allow the user to interact with the application.
	 * 
	 * @param frame			Object of the JFrame which will be modified.
	 * @param user			Name of the user.
	 * @param clientcomm	Object which allows communication with the server.
	 */
	public MicroTraderOnlineGui(final JFrame frame, final String user, ClientComm clientcomm) {
		this.clientcomm = clientcomm;
		this.user = user;
		this.frame = frame;


		frame.setLayout(new BorderLayout());
		frame.setSize(new Dimension(900,650));
        frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		marketscrollPane.setViewportView(marketList);
		historyscrollPane.setViewportView(historyList);
	
		
		marketModel.addElement("ola1");
		marketModel.addElement("ola2");
		marketModel.addElement("ola3");
		historyModel.addElement("adeus");
		
		buyButton = new JButton("Buy");
		sellButton = new JButton("Sell");
		logoutButton = new JButton("Logout");
		
		logoutButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				getClientComm().disconnect();
				frame.dispose();
			}
			
		});
		
		sellButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0){
				
					
				MicroTraderOrder mto = new MicroTraderOrder(getClientComm(), user, false, historyList.getSelectedValue(),historyModel);
				
			}
		});

		
		buyButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
					
					MicroTraderOrder mto = new MicroTraderOrder(getClientComm(), user, true, marketList.getSelectedValue(),historyModel);
					System.out.println("BUY" );	
				
			}
			
		});

		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.add(buyButton);
		buttonPanel.add(sellButton);
		buttonPanel.add(logoutButton);

		historyPanel.setLayout(new BorderLayout());
		marketPanel.setLayout(new BorderLayout());
		historyPanel.add(historyscrollPane, BorderLayout.CENTER);
		marketPanel.add(marketscrollPane, BorderLayout.CENTER);
        
		frame.setVisible(true);
		frame.validate();
		frame.repaint();
		
		frame.add(historyPanel, BorderLayout.WEST);
		frame.add(marketPanel, BorderLayout.EAST);
		frame.add(buttonPanel, BorderLayout.CENTER);

		
	}
	
	/**
	 * Getter of the Object
	 * @return	Object that allows communication client-server
	 */
	private ClientComm getClientComm(){ return clientcomm;}

	/**
	 * Adds new order in the list of the previous JFrame.
	 * @param x		Information about the order placed.
	 */
	public void setNewPost(String x){
		marketModel.addElement(x);
		frame.repaint();
		frame.revalidate();
	}



}

